package analysis;

import common.AnalysisWare;
import common.Constants;
import common.DataAnalysis;
import util.FileUtil;
import util.remote.RemoteConnectResult;
import util.remote.RemoteType;

import java.io.IOException;
import java.util.*;

/**
 * 恩爱小窝数值修复
 * <p>
 * Created by J.Chen on 2017/5/25.
 */

@AnalysisWare(confCode = "Love_Bug", desc = "小窝bug修复", remote = {RemoteType.qk, RemoteType.qq})
public class LoveHouseDataBugFix extends DataAnalysis {
    public LoveHouseDataBugFix(RemoteConnectResult result, String confCode) {
        super(result, confCode);
    }

    @Override
    public void execute() {
        //获取所有的小窝

        List<Map<String, String>> info = result.getMysqlUtil().selectList("select * from love_house_info");
        Map<String, Map<String, String>> infoMap = new HashMap<>();
        for (Map<String, String> map : info) {
            infoMap.put(map.get("houseCode"), map);
        }

        Integer[] v = new Integer[]{601061, 601113, 601114, 601027, 601002, 601003, 601005, 601006, 601007, 601008, 601009, 601026, 601027, 601028, 601060, 601061, 602030, 602040, 602042};
        List<Integer> gameGold = Arrays.asList(v);
        String filePath = "E:\\SecureCRT\\合并.txt";
        List<String> list = null;
        try {
            list = FileUtil.readFile(filePath);
        } catch (IOException e) {
            e.printStackTrace();
        }
        List<Map<String, String>> mapList = new ArrayList<>();
        for (String value : list) {
            String[] ss = value.split(",");
            Map<String, String> map = new HashMap<>();
            for (String s : ss) {
                String[] s1 = s.split(":", 2);
                map.put(s1[0].trim(), s1[1].trim());

            }
            mapList.add(map);
        }
        Map<String, Integer> loveResult = new HashMap<>();
        Map<String, Integer> blessResult = new HashMap<>();

        FileUtil fileUtilLove = new FileUtil(Constants.getActOutputDir(result.getRemoteConf(), "love"));
        FileUtil fileUtilBless = new FileUtil(Constants.getActOutputDir(result.getRemoteConf(), "bless"));
        for (Map<String, String> map : mapList) {
            String goodsId = map.get("goodsId");
            if (gameGold.contains(Integer.parseInt(goodsId))) {
                String ktvId = map.get("ktvId");
                String sender = map.get("sender");
                String receiver = map.get("receiver");
                String m = infoMap.get(ktvId).get("masterId");
                String s = infoMap.get(ktvId).get("spouseId");
                if ((sender.equals(m) && receiver.equals(s)) || (sender.equals(s) && receiver.equals(m))) {
                    if (loveResult.containsKey(ktvId)) {
                        loveResult.put(ktvId, loveResult.get(ktvId) + Integer.parseInt(map.get("price")) / 100);
                    } else {
                        loveResult.put(ktvId, Integer.parseInt(map.get("price")) / 100);
                    }
                } else {
                    if (blessResult.containsKey(ktvId)) {
                        blessResult.put(ktvId, blessResult.get(ktvId) + Integer.parseInt(map.get("price")) / 100);
                    } else {
                        blessResult.put(ktvId, Integer.parseInt(map.get("price")) / 100);
                    }
                }


            }
        }
        for (Map.Entry<String, Integer> en : loveResult.entrySet()) {
            fileUtilLove.writeln("|", en.getKey(), infoMap.get(en.getKey()).get("fullNumLove"), "" + en.getValue());
        }
        fileUtilLove.close();
        for (Map.Entry<String, Integer> en : blessResult.entrySet()) {
            fileUtilBless.writeln("|", en.getKey(), infoMap.get(en.getKey()).get("fullNumBless"), infoMap.get(en.getKey()).get("remainNumBless"), "" + en.getValue());
        }
        fileUtilBless.close();
    }
}
